package org.social.manager;

import java.util.Date;
import java.util.Map;

import org.bean.GiftCodeDTO;
import org.error.GameErrorCode;
import org.social.db.bean.GiftCode;
import org.social.db.bean.GiftPackage;
import org.social.db.cache.GiftCodeCache;

import com.google.common.collect.Maps;
import com.sojoys.artifact.manager.JsonManager;
import com.sojoys.artifact.tools.ToolError;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * Created by fxf on 2018-1-11.
 */
public class GiftCodeManager {


    public static void addGiftCode(String code,String name,int giftPackageId, int limitCount, String platform, int channelId, Date startDate,Date endDate, String remark){
        GiftCode giftCode = new GiftCode(0, giftPackageId, name, code, limitCount, platform, channelId, startDate, endDate, Maps.newHashMap(), remark);
        giftCode.insert();
    }

    /**
     * 使用礼包
     * @param pid
     * @param osName
     * @param channelId
     * @param code
     * @return
     */
    public static GiftCodeDTO gainGift(int pid,String osName,int channelId,String code){
        GiftCode giftCode = GiftCodeCache.me().getByCode(code);
        
        ToolError.isAndTrue(GameErrorCode.GIFT_CODE_ERR,"礼包码错误",giftCode == null);
        Map gainLog = giftCode.getGainLog();
        if (gainLog==null) {
        	gainLog = Maps.newHashMap();
        	giftCode.setGainLog(gainLog);
		}
        ToolError.isAndTrue(GameErrorCode.GIFT_CODE_ALREADY_GAIN,"你已经使用过该礼包码",gainLog.containsKey(pid));
        
        ToolError.isAndTrue(GameErrorCode.GIFT_CODE_GAIN_OVER,"礼包码被领完",giftCode.getLimitCount() <= gainLog.size());
        long now = System.currentTimeMillis();
        if (giftCode.getStartTime() !=null) {
        	ToolError.isAndTrue(GameErrorCode.GIFT_CODE_BEFORE_START,"礼包码未生效",giftCode.getStartTime().getTime() > now);	
		}
        if (giftCode.getEndTime()!=null) {
        	ToolError.isAndTrue(GameErrorCode.GIFT_CODE_AFTER_END,"礼包码已失效",giftCode.getEndTime().getTime() < now);	
		}
        
        if (!StrUtil.isBlank(giftCode.getPlatform())) {
        	ToolError.isAndTrue(GameErrorCode.GIFT_CODE_PLATFORM_ERR,"平台错误", !giftCode.getPlatform().equals(osName));	
		}
        if (giftCode.getChannelId()!=0) {
        	ToolError.isAndTrue(GameErrorCode.GIFT_CODE_CHANNEL_ERR,"渠道错误", giftCode.getChannelId() != channelId);	
		}
        
        gainLog.put(pid, now);
        giftCode.update();
        return buildGiftCodeDTO(giftCode);
    }

    public static void deleteGiftCode(String code){
        GiftCode giftCode = GiftCodeCache.me().getByCode(code);
        giftCode.delete();
    }

    public static GiftCode queryGiftCode(String code){
        return GiftCodeCache.me().getByCode(code);
    }

    private static GiftCodeDTO buildGiftCodeDTO(GiftCode giftCode){
    	GiftPackage giftPackage = giftCode.getGiftPackagePkGiftPackageId();
    	Map itemPackage = JsonManager.me().getJson().parse(giftPackage.getContent(), Map.class);
        GiftCodeDTO dto = new GiftCodeDTO();
        return dto.setItemMap(itemPackage);
    }
}
